package com.ls.sync;

import java.util.concurrent.TimeUnit;

public class DeadLock {
    static Object a = new Object();
    static Object b = new Object();

    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (a){
                System.out.println(Thread.currentThread().getName()+" 持有a ,试图获取b");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b){
                    System.out.println(Thread.currentThread().getName()+" 持有ab");
                }
            }

        }, "aa").start();
        new Thread(() -> {
            synchronized (b){
                System.out.println(Thread.currentThread().getName()+" 持有b ,试图获取a");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a){
                    System.out.println(Thread.currentThread().getName()+" 持有ab");
                }
            }

        }, "bb").start();
    }
}
